﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace FileProtector.Libraries
{
    public class SettingParams
    {
        //Properties
        public string Name { get; set; }
        public string Password { get; set; }
        public string VaultDirectory { get; set; }
        public string ApplicationDirectory { get; set; }
        public string EncryptedFolder { get; set; }

        //Constants
        public string strDelim = "!!!!----////@@@@12345678901234567890abcdefghijklmnopqrstuvwxyzan1rban";

        //Private
        private string EncPassword = "An1rbanB567tach2ya04mmyy70";
        private string Folder = "___temp_." + SystemExtension.ZIP_FOLDER;

        //Constructor
        public SettingParams(string AppDir)
        {
            this.ApplicationDirectory = AppDir;

            if (File.Exists(AppDir + "\\settings.ini"))
            {
                try
                {
                    string str = File.ReadAllText(AppDir + "\\settings.ini");

                    if (!string.IsNullOrEmpty(str))
                    {
                        //Decrypt the text
                        str = EncryptDecrypt.Decrypt(str, EncPassword);

                        string[] strarr = System.Text.RegularExpressions.Regex.Split(str, strDelim);

                        if (strarr.Length > 2)
                        {
                            Name = strarr[0];
                            Password = strarr[1];
                            VaultDirectory = strarr[2];
                            EncryptedFolder = VaultDirectory + "\\" + this.Folder;
                        }
                    }
                }
                catch { }
            }
        }
                
        //Save Params
        public void SaveSettings(string str)
        {
            if (!File.Exists(ApplicationDirectory + "\\settings.ini"))
                File.Create(ApplicationDirectory + "\\settings.ini").Close();

            str = EncryptDecrypt.Encrypt(str, EncPassword);

            File.WriteAllText(ApplicationDirectory + "\\settings.ini", str);
        }

        //Create Directory
        public void CreateDirectory(string path)
        {
            if (!Directory.Exists(path + "\\" + this.Folder))
                Directory.CreateDirectory(path + "\\" + this.Folder);
        }

        //Import File
        public void AddFile(string strFile)
        {
            try
            {
                EncryptDecrypt.Encrypt(strFile, EncryptedFolder + "\\" + Path.GetFileName(strFile), EncPassword);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Encrypt SIngle
        public void EncryptSingle(string strSource, string strTemp)
        {
            try
            {
                EncryptDecrypt.Encrypt(strSource, strTemp + "\\" + Path.GetFileName(strSource), EncPassword);
                File.Delete(strSource);
                File.Copy(strTemp + "\\" + Path.GetFileName(strSource), strSource);
                //File.Replace(strTemp + "\\" + Path.GetFileName(strSource), strSource, Path.GetDirectoryName(strSource) + "\\" + Path.GetFileNameWithoutExtension(strSource) + ".bak");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Decrypt SIngle
        public void DecryptSingle(string strSource, string strTemp)
        {
            try
            {
                EncryptDecrypt.Decrypt(strSource, strTemp + "\\" + Path.GetFileName(strSource), EncPassword);
                File.Delete(strSource);
                File.Copy(strTemp + "\\" + Path.GetFileName(strSource), strSource);
                //File.Replace(strTemp + "\\" + Path.GetFileName(strSource), strSource, Path.GetDirectoryName(strSource) + "\\" + Path.GetFileNameWithoutExtension(strSource) + ".bak");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
